package com.zecan.blog.service.impl;

import com.zecan.blog.common.status.UserStatus;
import com.zecan.blog.controller.exception.UserException;
import com.zecan.blog.dao.FriendMapper;
import com.zecan.blog.dao.UserMapper;
import com.zecan.blog.entity.BlogUser;
import com.zecan.blog.entity.Friend;
import com.zecan.blog.service.FriendService;
import com.zecan.blog.utils.StringUtils;
import com.zecan.blog.vo.friend.FriendVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zecan
 * @Description:
 * @ClassName: FriendServiceImpl
 * @date 2022/12/18 14:06
 */

@Service
public class FriendServiceImpl implements FriendService {


    @Resource
    private UserMapper userMapper;

    @Resource
    private FriendMapper friendMapper;


    @Override
    public List<FriendVo> getUserFriendList(Integer userId) {
        // 判断当前用户是否存在
        BlogUser userByUserId = userMapper.findUserByUserId(userId);
        if (userByUserId == null) {
            throw new UserException(UserStatus.USER_NOT_FOUND);
        }
        List<Friend> userFriendList = friendMapper.getUserFriendList(userId);
        List<FriendVo> list = new ArrayList<>();

        userFriendList.forEach(entity -> {
                   list.add(
                           FriendVo.builder()
                                   .friendUserId(entity.getFriendUserId())
                                   .friendAddedUserId(entity.getFriendAddedUserId())
                                   .friendAvatarUrl(entity.getFriendAvatarUrl())
                                   .friendNickName(entity.getFriendNickName())
                                   .lastMessage(entity.getLastMessage())
                                   .lastMessageTime(StringUtils.shortDate(entity.getLastMessageTime()))
                                   .isNewMsg(false)
                                   .build()
                   );
        });

        return list;
    }

    @Override
    public List<FriendVo> getAllUser() {
        List<BlogUser> userInfo = userMapper.findUserInfo();
        List<FriendVo> list = new ArrayList<>();
        userInfo.forEach(entity -> {
            list.add(
                    FriendVo.builder()
                            .friendUserId(Math.toIntExact(entity.getUserId()))
                            .friendAvatarUrl(entity.getUserAvatarUrl())
                            .friendNickName(entity.getUserNickname())
                            .friendUsername(entity.getUsername())
                            .build()
            );
        });
        return list;
    }

    @Override
    public void addFriend(Integer sendAddUserId, Integer addUserId) {
        Integer friendIsExist = friendMapper.getFriendIsExist(sendAddUserId, addUserId);
        if(friendIsExist != null) {
            throw new UserException(UserStatus.USER_ADD_FRIEND_IS_EXIST);
        }
        // TODO Mybatis 技术过于弱, 无法进行一条语句插入两个数据.. 先这样了
        friendMapper.addFriend(sendAddUserId,addUserId,StringUtils.dateFormat(new Date()));
        friendMapper.addFriend(addUserId,sendAddUserId,StringUtils.dateFormat(new Date()));

    }
}
